From: Anders Tiberg <anders.tiberg@telia.com>
To: <filearchive@ticalc.org>
Subject: PrList v3.14 and other programs
Date: january 10, 2009  





 This is a collection of programs that are aimed for people that want to write in Basic.
One of the types of programs best suited to start out with are primefactor programs. They
have all the ingredients of a real program i.e. the problem isn't solved in one single
step like in a formula program that solves for the roots of the quadratic equation, but
the program runs in a repeated loop, yet simple to understand:




 PrimeX
:Prompt X
:1->P                   -> is the STO-button.
:Lbl 1
:P+(P>2)+1->P          (P>2) becomes one when P>2, until then nill.
:Lbl 2
:If P^2>X              use the built in squarefunction instead of ^2 throughout.
:Goto 3                
:If fPart(X/P          If fPart(X/P) differs from nill.
:Goto 1
:X/P->X
:Pause P
:Goto 2
:Lbl 3:X


 As simple as possible this program is very slow. The prime 302 837 takes 12 seconds to verify.



 PrimeY
:Promt X
:2->P
:Goto 2
:Lbl 1
:X/P->X
:Disp P
:Lbl 2
:If P<Xnot(fPart(X/P       If X>P and fPart(X/P)=0
:Goto 1
:P+(P>2)+1->P
:If P^2<=X              <= means smaller than or equal to.
:Goto 2
:X


 With only two labels this program is even slower.




 PrimeZ
:Prompt X
:2->P
:While P^2<=X
:If fPart(X/P
:Then
:P+(P>2)+1->P
:Else
:X/P->X
:Disp P
:End
:End:X


 Not using lables saves bytes. The speed is the same as PrimeX's though.





 Prime1
:Prompt X
:2->P
:Lbl 1
:For(P,P,\/(X                 \/(X means the squareroot of X
:If fPart(X/P
:Then
:P+(P>2)->P
:Else
:X/P->X
:Disp P
:Goto 1
:End
:End:X


 Using the For-command this program takes nine seconds to verify the test number.




 Prime2
:Prompt X
:2->P
:While 2<Xnot(fPart(X/2
:Lbl 1
:X/P->X
:Disp P
:End
:For(P,P+(P=2),\/(X),2)
:If fPart(X/P
:End
:If P^2<=X                 
:Then                           Using If Then prevents that P is counted up unduly. The End-
:Goto 1                         command is not needed.
:Else:X


 Prime2 only needs a little more than five seconds to show that 302 837 is a prime. Note that
if the right hand bracket in the For-command is removed the time increases to eleven sec.


 PrimeQ
:Prompt X
:2->Q
:While 2<Xnot(fPart(X/2
:Lbl 1
:X/Q->X
:Disp Q
:End
:For(P,Q+(Q=2),\/(X),2)
:If fPart(X/P
:End
:P->Q
:X->P
:If Q^2<=X
:Goto 1
:X


 PrimeQ doesn't skip the End-command in the For-loop, but is for some reason a bit slower.



 Prime3
:ClrHome
:Prompt X
:2->P
:1+not(fPart(X/2->I
:1->J
:2->K
:ClrHome
:Disp X
:While not(fPart(X/2
:Lbl 1
:X/P->X
:Output(I,K,P
:If J>1
:Then
:iPart(log(P->Q
:Output(I,K+1+Q,"^
:Output(I,K+2+Q,J
:End
:J+1->J
:End
:I+1->I
:If I>8
:Then
:2->I
:8->K
:End
:1->J
:For(P,P+(P>2)+1,\/(X),2
:While not(fPart(X/P
:Goto 1
:End
:End
:If X>1
:Output(I,K,X



 This program shows the prime factors in columns with their power. Time is six seconds.



 Prime4
:ClrHome
:Prompt A
:For(B,2,4)
:If fPart(A/B
:End
:For(B,B,\/(A),6)
:If fPart(A/B) and fPart(A/(B+2
:End
:If B^2>A
:Then
:Disp "","IS A PRIME
:Else
:Disp "","COMPOSITE
:B+2(0~fPart(A/B               ~ means differs from.



 Prime4 doesn't divide with numbers wich has a factor 2 or 3. Time is about 3.5 seconds.




 Prime5-1
:ClrHome
:Promt X
:For(P,2,4
:While P<Xnot(fPart(X/P
:X/P->X
:Disp P
:End
:Lbl 1
:End
:For(P,P,\/(X),6)
:If fPart(X/P) and fPart(X/(P+2
:End
:If P^2<=X
:Then
:P+2(0~fPart(X/P->F
:X/F->X
:Disp F
:Goto 1
:End:X



 Prime5-2
:ClrHome
:Prompt X
:For(Q,2,4
:While Q<Xnot(fPart(X/Q
:Lbl 1
:Q+2(0~fPart(X/Q->F              If fPart(X/Q)~0 F becomes two more than Q else the same.
:X/F->X
:Disp F
:End
:End
:For(P,Q,\/(X),6)
:If fPart(X/P) and fPart(X/(P+2
:End
:If P^2<=X
:Then
:P->Q
:X->P
:Goto 1
:Else:X



 Prime5-1 and -2 are just as fast as Prime4 and shows the factors.




 Prime6
:ClrHome
:Prompt A
:For(B,2,6)
:If fPart(A/B
:End
:For(B,B,\/(A),30)
:If min(fPart(A/(B+{0,4,6,10,12,16,22,24    If all fraction parts differ from nill.
:End
:If B^2>A
:Then
:Disp "","IS A PRIME
:Else
:Disp "","COMPOSITE
:For(B,B,B+24,2)
:If fPart(A/B
:End
:B


 Prime6 uses numbers that has no factor 2,3 or 5. Time for 302 837 is two seconds.




 Prime7
:ClrHome
:Prompt X
:7->G
:{X->L1
:For(Q,2,14
:While Q<Xnot(fPart(X/Q
:Lbl 1
:X/Q->X
:Disp Q
:Q->L1(1+dim(L1
:End
:End
:For(F,G,\/(X),30)
:If min(fPart(X/(F+{0,4,6,10,12,16,22,24
:End
:If F^2<=X
:Then
:For(P,max(F,Q),F+24,2
:If not(fPart(X/P
:Then
:P->Q
:X->P
:F->G
:X->F
:End
:End
:Goto 1
:End
:X->L1(1+dim(L1
:ClrHome
:Disp "",dim(L1)-1
:L1




 PRIME8
:ClrHome
:Promt X
:7->G
:4->O
:{X->L1
:{0,4,6,10,12,16,22,24->L2
:For(Q,2,14
:While Q<Xnot(fPart(X/Q
:Lbl 1
:X/Q->X
:Disp Q
:Q->L1(1+dim(L1
:End
:End
:For(F,G,\/(X),30)
:If min(fPart(X/(F+L2
:End
:If F^2<=X
:Then
:If F~G
:1->O
:For(P,O,8
:If not(fPart(X/(F+L2(P
:Then
:F+L2(P->Q
:P->O
:8->P
:F->G
:X->F
:End
:End
:Goto 1
:End
:X->L1(1+dim(L1
:ClrHome
:Disp "",dim(L1)-1
:L1







 The four programs below are close relatives of prime factor programs since they simplify square roots:



 RAD13
:ClrHome
:Input "ROOT OF?",N
:2->E
:1->G
:While not(fPart(N/4
:Lbl 1
:N/E^2->N
:EG->G
:End
:For(F,E+(E=2),\/(N),2)     Don't leave out this right hand bracket!
:If fPart(N/F^2
:End
:F->E
:N->F
:If E^2<=N
:Goto 1
:Disp G,"*\/(
:N



 RAD14
:ClrHome
:Input "ROOT OF?",N
:2->F
:1->G
:While not(fPart(N/4
:Lbl 1
:N/F^2->N
:FG->G
:End
:For(F,F+(F>2)+1,\/(N),2)    Don't leave out this right hand bracket!
:If fPart(N/F^2
:End
:While not(fPart(N/F^2
:Goto 1
:End
:Disp G,"*\/(
:N



 RAD15
:ClrHome
:Input "ROOT OF?",N
:2->F
:1->G
:1->H
:While not(fPart(N/2
:Lbl 1
:If fPart(N/F^2
:Then
:N/F->N
:FH->H
:Else
:N/F^2->N
:FG->G
:End
:End
:For(F,F+(F>2)+1,3\/(N+1),2      The third root of N+1 (MATH 4)
:While not(fPart(N/F
:Goto 1
:End
:End
:If fPart(\/(N
:Then
:HN->N
:Else
:G\/(N->G
:H->N
:End
:Disp G,"*\/(
:N



 RADICAL
:ClrHome
:Input "ROOT OF?",W
:{0->L1
:1->A
:2->X
:While not(fPart(W/2
:Lbl 1
:X->L1(A
:W/X->W
:A+1->A
:End
:For(X,X+(X>2)+1,3\/(W+1),2       The third root of W+1 (MATH 4)
:While not(fPart(W/X
:Goto 1
:End
:End
:If W~1
:Then
:If fPart(\/(W
:Then
:W->L1(A
:Else
:\/(W->L1(A
:Ans->L1(A+1
:End
:End
:1->R
:1->W
:1->H
:For(X,1,dim(L1
:L1(X->G
:If G=H
:Then
:WH->W
:1->H
:Else
:RH->R
:G->H
:End
:End
:RH->R
:Output(3,1,W
:1->A
:While W
:int(W/10->W
:A+1->A
:End
:Output(3,A"\/(
:Output(3,A+1,R
:Disp "","
:L1




 The following programs are prime factor programs:


 PrimeN1
:ClrHome
:Disp "FIRST PRIME>N,IF","N<=-6 OR N>=3   >= means bigger than or equal to.
:Prompt N
:N-not(fPart(N/2->N
:Repeat F<P
:N+2->N
:abs(N->P
:For(F,3,\/(P),2
:If not(fPart(P/F
:P->F
:End:End
:Disp "PRIME FOUND=":N






 PrimeN2
:ClrHome
:Disp "FIRST PRIME>N,IF","N<=-8 OR N>=5
:Prompt N
:N-not(fPart(N/2->N
:Repeat F<P
:N+2->N
:N+2not(fPart(N/3->N
:abs(N->P
:For(F,5,\/(P),6)
:If fPart(P/F) and fPart(P/(F+2
:End
:If F^2<=P
:P->F
:End
:If F>P
:End
:Disp "PRIME FOUND=":N


 Note the extra End-command in PrimeN2. The calculator can skip an End-command
a limited number of times, due to how much memory that is used, before it stops
with an ERR:MEMORY. It is important in a program to see to it that this doesn't happen.







 The following eight programs gives the prime numbers in an interval:


 CPRIMES
:Input A
:{2->L1
:For(B,3,A,2
:If min(fPart(B/L1
:B->L1(1+dim(L1
:End
:L1

 This is the simplest program to give the prime numbers from 1 to a number you specify.



 CPRIMES1
:Input A
:{2->L1
:For(B,3,\/(A),2
:If min(fPart(B/L1
:B->L1(1+dim(L1
:End
:L1->L2
:For(B,B,A,2
:If min(fPart(B/L2
:B->L1(1+dim(L1
:End
:L1

 The program gets much faster if you only use the primes onto the square root of A.



 CPRIMES2
:ClrHome
:Input "MIN?",C
:Input "MAX?",L
:{2->L1
:L1->L2
:For(B,3,\/(L),2
:If min(fPart(B/L1
:Then
:B->L1(1+dim(L1
:B->L2((L2(1)>=C)+dim(L2            If L2(1) is smaller than C it is written over.
:End
:End
:max(B,1+2iPart(C/2
:For(B,Ans,L,2
:If min(fPart(B/L1
:B->L2((L2(1)>=C)+dim(L2
:End
:Disp dim(L2
:L2

 With a slight modification the program gives the primes in an interval of your choice. 



 Print1
:ClrHome
:Input "MIN?",C
:Input "MAX?",L
:{2,3->L1
:If C>2
:{3->L1
:max(5,1+2iPart(C/2
:For(P,Ans,L,2
:For(F,3,\/(P),2
:If not(fPart(P/F
:P->F
:End
:If F<P
:P->L1((L1(1)>=C)+dim(L1
:End
:If C>L1(1
:{0->L1
:Disp dim(L1
:L1





 Print2
:ClrHome
:Input "MIN?",C
:Input "MAX?",L
:{2->L1
:For(P,3,5,2
:P->L1((L1(1)>=C)+dim(L1
:End
:max(7,1+2iPart(C/2
:For(P,Ans,L,2
:P+2not(fPart(P/3->P
:For(F,5,\/(P),6)
:If fPart(P/F) and fPart(P/(F+2
:End
:If F^2>P
:Then
:P->L1((L1(1)>=C)+dim(L1
:Else
:P->F
:End
:End
:If L>P
:End
:If C>L1(1
:{0->L1
:If L<max(L1
:dim(L1)-1->dim(L1
:Disp dim(L1
:L1





 Print3
:ClrHome
:Input "MIN?",C
:Input "MAX?",L
:{2->L1
:For(P,3,7,2
:P->L1((L1(1)>=C)+dim(L1
:End
:max(11,1+2iPart(C/2
:For(P,Ans,L,2
:P+2not(fPart(P/3->P
:P+2not(fPart(P/5->P
:P+2not(fPart(P/3->P
:For(F,7,\/(P),30)
:If min(fPart(P/(F+{0,4,6,10,12,16,22,24
:End
:If F^2>P
:Then
:P->L1((L1(1)>=C)+dim(L1
:Else
:P->F
:End
:End
:If L>P
:End
:If C>min(L1
:{0->L1
:If L<max(L1
:dim(L1)-1->dim(L1
:Disp dim(L1
:L1


 Print3 is the fastest all over of these last five programs in relation to memory usage.





 PrList v2.21
:ClrHome
:Input "MIN?",C
:Input "MAX?",L
:{2->L1
:For(P,3,5,2
:P->L1((L1(1)>=C)+dim(L1
:End
:If L-C<\/(2L
:Then
:Disp "(1)
:max(7,1+2iPart(C/2
:For(P,Ans,L,2
:For(F,3,\/(P),2
:If not(fPart(P/F
:P->F
:End
:If F<P
:P->L1((L1(1)>=C)+dim(L1
:End
:Else
:Disp "(2)
:{5->L2
:L2->L3
:5->P
:\/(L->Q
:Q->R
:7^2->S
:While P<L
:For(P,P,Q,2
:For(P,P,P+2,2
:While P>=S
:1+dim(L3->I
:L2(I->L3(I
:L2(I+1)^2->S
:End
:If min(fPart(P/L3
:Then
:If P<=R
:P->L2(1+dim(L2
:P->L1((L1(1)>=C)+dim(L1
:End
:End
:End
:If P<C
:P+6iPart((C-P)/6->P
:L->Q
:P->L2(1+dim(L2
:End
:End
:If C>min(L1
:{0->L1
:If L<max(L1
:dim(L1)-1->dim(L1
:Disp dim(L1
:L1



 The inspiration for PrList, Prime6, CPRIMES and Print3 comes from a little program by Charles
J. Srstka, csrstka@ku.edu: Find Prime Numbers 1.0 (primenum.zip) in the TI-83 BASIC Math
directory. The idea of it is that a number is divided by the whole list of prime numbers found
from two and on, to see if it is a prime. It was Charles idea to use {2->L1 and P->L1((L1(1)>=C)
+ dim(L1 in PrList,(to solve the issue of the TI-83 not being able to handle the command X->
L1(1+ dim(L1 , unless the list has at least one number) thus overwriting the first element.
PrList was first uploaded on june 13, 2000 to the TI-83 BASIC Math directory by Charles.




 PrList v3.14
:ClrHome
:Input "MIN?",C
:Input "MAX?",L
:{2->L1
:For(P,3,7,2
:P->L1((L1(1)>=C)+dim(L1
:End
:If L-C>=\/(5L
:Goto 1
:Disp "(1)
:max(11,1+2iPart(C/2
:For(P,Ans,L,2
:P+2not(fPart(P/3->P
:For(F,5,\/(P),6)
:If fPart(P/F) and fPart(P/(F+2
:End
:If F^2>P
:Then
:P->L1((L1(1)>=C)+dim(L1
:Else
:P->F
:End
:End
:If L>P
:End
:Goto 2
:Lbl 1
:Disp "(2)
:{7->L2
:L2->L3
:{0,4,6,10,12,16,22,24->L4
:7->P
:\/(L->Q
:Q->R
:11^2->S
:While P<L
:For(P,P,Q,30
:8->I
:While L<P+L4(I
:I-1->I
:End
:For(I,1,I
:P+L4(I->O
:While O>=S
:1+dim(L3->J
:L2(J->L3(J
:L2(J+1)^2->S
:End
:If min(fPart(O/L3
:Then
:If O<=R
:O->L2(1+dim(L2
:O->L1((L1(1)>=C)+dim(L1
:End
:End
:End
:If P<C
:P+30iPart((C-P)/30->P
:L->Q
:P->L2(1+dim(L2
:End
:Lbl 2
:If C>min(L1
:{0->L1
:If L<max(L1
:dim(L1)-1->dim(L1
:Disp dim(L1
:L1




                        Questions and/or input write to: anders.tiberg@telia.com




